//
//  StringEncrypt.m
//  KLGenerateSpamCode
//
//  Created by Rowling on 2023/4/25.
//  Copyright © 2023 GAEA. All rights reserved.
//

#import "StringEncrypt.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation StringEncrypt
// 加密方法
+ (NSString*)encrypt:(NSString*)plainText key:(NSString *)key iv:(nullable NSString *)iv{
    NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    size_t plainTextBufferSize = [data length];
    const void *vplainText = (const void *)[data bytes];
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    bufferPtrSize = 1024 * (plainTextBufferSize >> 10) + 1024;
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    CCOptions op = iv == nil ? (kCCOptionECBMode | kCCOptionPKCS7Padding) : kCCOptionPKCS7Padding;
//    const void *vinitVec = (const void *) [key UTF8String];
    ccStatus = CCCrypt(kCCEncrypt,
                       kCCAlgorithmAES,
                       op,
                       [[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
                       kCCKeySizeAES128,
                       iv == nil ? NULL : [[iv dataUsingEncoding:NSUTF8StringEncoding] bytes],
                       vplainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);
    NSString * ciphertext = @"";
    if (ccStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:bufferPtr length:(NSUInteger)movedBytes];
        NSData * S64tring = [data base64EncodedDataWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
        ciphertext = [[NSString alloc] initWithData:S64tring encoding:kCFStringEncodingUTF8];
    }
    free(bufferPtr);
    return ciphertext;
}

// 解密方法
+ (NSString*)decrypt:(NSString*)encryptText key:(NSString *)key iv:(nullable NSString *)iv{
    NSData * base64Data = [encryptText dataUsingEncoding:kCFStringEncodingUTF8];
    if (base64Data == nil) {return @"";}
    NSData *encryptData = [[NSData alloc] initWithBase64EncodedData:base64Data options:NSDataBase64DecodingIgnoreUnknownCharacters]; //[self hexStringToData:encryptText];//[GTMBase64 decodeData:[encryptText dataUsingEncoding:NSUTF8StringEncoding]];
    size_t plainTextBufferSize = [encryptData length];
    const void *vplainText = [encryptData bytes];
    CCOptions op = iv == nil ? (kCCOptionECBMode | kCCOptionPKCS7Padding) : kCCOptionPKCS7Padding;
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    bufferPtrSize = 1024 * (plainTextBufferSize >> 10) + 1024;
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
//    const void *vinitVec = (const void *) [key UTF8String];

    ccStatus = CCCrypt(kCCDecrypt,
                       kCCAlgorithmAES,
                       op,
                       [[key dataUsingEncoding:NSUTF8StringEncoding] bytes],
                       kCCKeySizeAES128,
                       iv == nil ? NULL : [[iv dataUsingEncoding:NSUTF8StringEncoding] bytes],
                       vplainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);

    NSString *result = @"";
    if (ccStatus == kCCSuccess) {
       result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr
                                                              length:(NSUInteger)movedBytes] encoding:NSUTF8StringEncoding];
    }
    free(bufferPtr);
    return result;
}
@end

@implementation MatchItem

@end

@implementation ScanSegment

@end

